// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 初始化用户信息
    this.initUserInfo()
  },

  // 初始化用户信息
  initUserInfo() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.globalData.userInfo = userInfo
      this.globalData.isLoggedIn = true
    } else {
      this.globalData.isLoggedIn = false
    }
  },

  // 保存用户信息
  saveUserInfo(userInfo) {
    const completeUserInfo = {
      ...userInfo,
      desc: userInfo.desc || '这个人很懒，什么都没写...',
      loginTime: new Date().toISOString()
    }
    
    // 保存到本地存储
    wx.setStorageSync('userInfo', completeUserInfo)
    this.globalData.userInfo = completeUserInfo
    this.globalData.isLoggedIn = true
    
    return completeUserInfo
  },

  // 退出登录
  logout() {
    wx.removeStorageSync('userInfo')
    this.globalData.userInfo = null
    this.globalData.isLoggedIn = false
  },

  globalData: {
    userInfo: null,
    isLoggedIn: false,
    needUpdateStats: false,
    switchToFollowTab: false,
    searchWordData: null,
    shareData: null
  }
})
